package org.bioversityinternational.mgbms.model.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.bioversityinternational.mgbms.model.dao.base.BaseDao;
import org.bioversityinternational.mgbms.model.domain.Contact;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component
public class ContactDao extends BaseDao<Contact, Integer>
{
	private static final Logger LOG = Logger.getLogger(ContactDao.class);
	
	public ContactDao() {
		super(Contact.class);		
        LOG.debug("ContactDao constructed");
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.SUPPORTS)
	public Contact getByEmail(String email) {
		return getByUniqueAttribute("email", email);
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<String> listDistinctTitles() {
		String sQuery = "select distinct title from " + type.getName();
		List<String> results = getSession().createQuery(sQuery).list();
        LOG.debug("hql: " + sQuery + " -> " + results.size() + " results");
		return results;
	}

	@Override
	public Integer getIdFromString(String sIdAsString) {
		return Integer.parseInt(sIdAsString);
	}
		
}

